﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OptimumInfra.ViewData;
using OptimumInfra.Models;
using System.Net.Mail;
using System.Net;


namespace OptimumInfra.Controllers
{
    [Authorize(Roles = "admin")]
    public class NewsletterController : Controller
    {
        //
        // GET: /Email/
        //
        // GET: /Order/
        private EmailRepository repo = new EmailRepository();
        public ActionResult Index()
        {
            ViewData["emails"] = this.repo.GetAllEmail();
            return View(ViewData);
        }
        //
        // GET: /Order/Details/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            Newsletter email = this.repo.GetEmailById(id);
            return View(email);
        }

        //
        // GET: /Order/Create
        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Order/Create

        [HttpPost]
        [Authorize(Roles = "admin")]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                Newsletter email = new Newsletter();
                UpdateModel(email);
                this.repo.UpdateEmail(email);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult SendNewsletter()
        {
            ViewDataNewsletter email = new ViewDataNewsletter();
            email.Receiver = this.repo.GetAllReceivers();
            return View(email);
        }

        [HttpPost]
        public ActionResult SendNewsletter(FormCollection collection)
        {
            try
            {
               ViewDataNewsletter mail = new ViewDataNewsletter();
                UpdateModel(mail);
                string emails = mail.Receiver;
                string sender = mail.Sender;
                MailMessage Email = new MailMessage(sender,emails);
                Email.Subject = mail.Subject;
                Email.Body = mail.Content;
                
              
                SmtpClient mailClient = new SmtpClient();
                NetworkCredential basicAuthenticationInfo = new NetworkCredential("optimuminfra", "optimuminfra123");
                mailClient.Host = "mail.optimum.vn";
                mailClient.UseDefaultCredentials = false;
                mailClient.Credentials = basicAuthenticationInfo;
                mailClient.Send(Email);
                TempData["info"] = "Newsletter sent successfully";
                return RedirectToAction("Index");
            }
            catch
            {
                TempData["info"] = "Error occured";
                return View();
            }
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id)
        {
            this.repo.DeleteEmail(id);
            return RedirectToAction("Index");
        }

        //
        // POST: /Order/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                this.repo.DeleteEmail(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
